<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>求分数之和</title>
</head>
<body>
    <script>
        /* 求出1-1/2 + 1/3 - 1/4 + 1/5....... 1/100的和 */

        //总和
        var total = 1;
        //加减次数
        var n = 1;

        for(var i = 2;i <= 100; i++){
            //各位分数
            var num = 1/i;
            //奇数次为减，欧数次为加
            if(n%2!=0){
                total -= num;
            }else{
                total += num;
            }
            n++;
        }

        console.log('1-1/2 + 1/3 - 1/4 + 1/5....... 1/100的和为' + total);
    </script>
</body>
</html>